<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <el-form :inline="true" class="demo-form-inline">
        <!-- 搜索框 -->
        <el-form-item>
          <el-input
            placeholder="请输入内容"
            prefix-icon="el-icon-search"
            v-model="queryInfo.query">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getAllUser">搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="toAddPage">添加商品</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格数据 -->
      <el-table
        :data="userList"
        style="width: 100%"
        border
        stripe
      >
        <el-table-column
          prop="goods_id"
          label="商品编号">
        </el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称"
          width="750px">
        </el-table-column>
        <el-table-column
          prop="goods_price"
          label="价格">
        </el-table-column>
        <el-table-column
          prop="goods_weight"
          label="重量">
        </el-table-column><el-table-column
        prop="add_time"
        label="创建时间">
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="mySlot">
          <el-tooltip class="item" effect="dark" content="编辑" placement="top">
            <el-button @click="editDialog(mySlot.row.id)" type="primary" icon="el-icon-edit" circle></el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除" placement="top">
            <el-button @click="delDialog(mySlot.row)" type="danger" icon="el-icon-delete" circle></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
      </el-table>

      <!--分页操作-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>

      <!-- 编辑商品操作 -->
      <el-dialog
        title="编辑商品信息"
        v-model="editUserVisible"
        width="60%"
        center>
        <el-form :model="updateForm" status-icon :rules="rules" ref="updateForm" class="demo-ruleForm" label-width="90px">
          <el-form-item label="商品名称" prop="goods_name">
            <el-input v-model="updateForm.goods_name"  disabled></el-input>
          </el-form-item>
          <el-form-item label="价格" prop="goods_price">
            <el-input v-model="updateForm.goods_price" ></el-input>
          </el-form-item>
          <el-form-item label="数量" prop="goods_number">
            <el-input v-model="updateForm.goods_number" ></el-input>
          </el-form-item>
          <el-form-item label="重量" prop="goods_weight">
            <el-input v-model="updateForm.goods_weight" ></el-input>
          </el-form-item>
          <el-form-item label="介绍" prop="goods_introduce">
            <el-input v-model="updateForm.goods_introduce" ></el-input>
          </el-form-item>
          <el-form-item label="图片" prop="pics">
            <el-input v-model="updateForm.pics" ></el-input>
          </el-form-item>
          <el-form-item label="商品的参数" prop="attrs">
            <el-input v-model="updateForm.attrs" ></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="editUserVisible = false">取 消</el-button>
            <el-button type="primary" @click="edit">确 定</el-button>
          </span>
        </template>
      </el-dialog>

      <!-- 删除用户操作 -->
      <el-dialog
        title="提示"
        v-model="delUserVisible"
        width="30%"
        center>
        <span>你确定要删除{{userName}}用户吗?</span>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="delUserVisible = false">取 消</el-button>
            <el-button type="primary" @click="del">确 定</el-button>
          </span>
        </template>
      </el-dialog>

    </el-card>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus'

export default {
  name: "goods",
  data(){
    return{
      currentPage4:4,
      userList: [],
      queryInfo:{
        query:'',
        pagenum:1, //当前第一页，每一页5条数据
        pagesize:5
      },
      total:0,
      addUserVisible:false,
      delUserVisible: false,
      editUserVisible:false,
      ruleForm:{
        goods_name:'',
        goods_cat:'',
        goods_price:'',
        goods_number:'',
        goods_weight:'',
        goods_introduce:'',
        pics:'',
        attrs:''
      },
      updateForm:{

      },
      rules:{
        username: [
          { required: true, message:'请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message:'请输入密码', trigger: 'blur' }
        ],
        email: [
          { required: true, message:'请输入邮箱', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message:'请输入手机号', trigger: 'blur' }
        ]
      },
      userId: 0,
      userName: ''
    }
  },
  created() {
    this.getAllUser();
  },
  methods:{
    //获取所有的商品数据
    getAllUser(){
      this.axios.get('goods',{
        params:this.queryInfo
      }).then(data=>{
        console.log(data);
        if(data.data.meta.status !== 200) return ElMessage.error(data.data.meta.msg);
        //有数据了
        this.userList = data.data.data.goods;
        this.total = data.data.data.total;
      }).catch(err=>{
        console.log(err);
      })
    },
    //点击添加商品
    toAddPage(){
      this.$router.push('/add');
    },
    //编辑商品
    editDialog(id){
      this.userId = id;
      this.editUserVisible = true;

      this.axios.get(`users/${this.userId}`).then(resp=>{
        //console.log(resp.data.data);
        this.updateForm = resp.data.data
      }).catch(err=>{
        console.log(err);
      })
    },
    edit(){
      this.axios.put(`users/${this.userId}`,{
        email:this.updateForm.email,
        mobile:this.updateForm.mobile
      }).then(resp=>{
        if(resp.data.meta.status !== 200) return ElMessage.error('修改失败');
        ElMessage.success('修改成功');
        this.editUserVisible = false;
        this.getAllUser();
      }).catch(err=>{
        console.log(err);
      })
    },
    //删除商品
    delDialog(row){
      this.userId = row.id;
      this.userName = row.username;
      this.delUserVisible = true;

    },
    del(){
      this.axios.delete(`users/${this.userId}`).then(resp=>{
        if(resp.data.meta.status !== 200) return ElMessage.error('删除失败');
        ElMessage.success('删除成功');
        this.delUserVisible = false;
        this.getAllUser();
      }).catch(err=>{
        console.log(err);
      })
    },
    //每一页几条数据
    handleSizeChange(val){
      this.queryInfo.pagesize = val;
      this.getAllUser();
    },
    //当前第几页
    handleCurrentChange(val){
      this.queryInfo.pagenum = val;
      this.getAllUser();
    },
    //关闭表单所做的事情
    resetForm(formName){
      this.$refs[formName].resetFields();
      this.addUserVisible = false;
    }
  }
}
</script>

<style scoped>

</style>
